package recallin.location;

import recallin.activities.MainActivity;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

/**
 * Filters the best location provider. Handles all location information.
 * @author philipmasek
 *
 */

public class LocationUtility {
	
	public static final int LOCATION_CHANGED = 12312;
	protected final String TAG = "locationUtility";
	private Handler handler;
	private Location location;
	private String provider;
	private LocationManager locationManager;
	private LocationListener locationListener;
	private MainActivity activity;
	private LocationListener gpsListener;
	private LocationListener networkListener;
	
	public LocationUtility(LocationManager locationManager, String provider, Handler handler, MainActivity activity) {
		this.locationManager = locationManager;
		this.provider = provider;
		this.handler = handler;
		this.activity = activity;
	}
	

	public void startLocationService(){
		setLocationManager(locationManager);
		
		gpsListener = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
			}
			@Override
			public void onProviderEnabled(String provider) {
			}
			@Override
			public void onProviderDisabled(String provider) {
			}
			@Override
			public void onLocationChanged(Location location) {
				MainActivity.setCurrentLocation(location);
				Log.d(TAG, location.getLatitude()+" "+location.getLongitude()+" : "+location.getProvider());
				if (getHandler()!=null){
					getHandler().sendEmptyMessage(LOCATION_CHANGED);
				}
			}
		};
		networkListener = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
			}
			@Override
			public void onProviderEnabled(String provider) {
			}
			@Override
			public void onProviderDisabled(String provider) {
			}
			@Override
			public void onLocationChanged(Location location) {
				getActivity().setCurrentLocation(location);
				Log.d(TAG, location.getLatitude()+" "+location.getLongitude()+" : "+location.getProvider());
				if (getHandler()!=null){
					getHandler().sendEmptyMessage(LOCATION_CHANGED);
				}
			}
		};
		setLocationListener(gpsListener);
		locationManager.addGpsStatusListener(new Listener() {
			@Override
			public void onGpsStatusChanged(int event) {
				switch (event) {
				case GpsStatus.GPS_EVENT_FIRST_FIX:
					locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
					break;
				case GpsStatus.GPS_EVENT_STARTED:
					if (!getProvider().equals(LocationManager.NETWORK_PROVIDER))
						locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, networkListener);
					Log.d(TAG, "event started");
					break;
				default:
					break;
				}

			}
		});
		
		if (getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
			locationManager.requestLocationUpdates(getProvider(), 0, 10, networkListener);
		}else
			locationManager.requestLocationUpdates(getProvider(), 0, 10, gpsListener);
	}
	
	public void stopLocationService(){
		locationManager.removeUpdates(gpsListener);
		locationManager.removeUpdates(networkListener);
	}

	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		if (provider.equals(LocationManager.NETWORK_PROVIDER))
			getLocationManager().removeUpdates(getLocationListener());
	}

	public LocationManager getLocationManager() {
		return locationManager;
	}

	public void setLocationManager(LocationManager locationManager) {
		this.locationManager = locationManager;
	}

	private LocationListener getLocationListener() {
		return locationListener;
	}

	private void setLocationListener(LocationListener locationListener) {
		this.locationListener = locationListener;
	}

	public MainActivity getActivity() {
		return activity;
	}

	public void setActivity(MainActivity activity) {
		this.activity = activity;
	}

	
}
